package com.ruipeng.shengyapo.service.impl;

import com.ruipeng.shengyapo.comm.Page;
import com.ruipeng.shengyapo.dao.CouponMapper;
import com.ruipeng.shengyapo.dao.OrderMapper;
import com.ruipeng.shengyapo.dao.UserMapper;
import com.ruipeng.shengyapo.model.UserInfo;
import com.ruipeng.shengyapo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tu_pei on 17/5/28.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CouponMapper couponMapper;

    @Override
    public Map<String, Object> getByUserId(int userId) {
        return userMapper.getByUserId(userId);
    }

    @Override
    public int updateUserInfo(Map<String, Object> params) {
        return userMapper.updateUserInfo(params);
    }

    @Override
    public List<Map<String,Object>> getUserTrip(int userId) {
        return userMapper.getUserTripPageList(userId);
    }

    @Override
    public Map<String, Object> getuserTripInfo(Map<String, Object> params) {
        return userMapper.getuserTripInfo(params);
    }

    @Override
    public int updateOrderTrDingInfo(Map<String, Object> params) {

//        //判断订单是否完成
//
//        int count = 0;
//
//        // 构建订单对象
//        Map<String, Object> orderParams = new HashMap<String, Object>();
//        // 用户id
//        Integer userId = (Integer) params.get("userId");
//
//        // 获取最近订单
//        Map<String,Object> orderData = orderMapper.getUserOrderByUserId(params).get(0);
//
//        // 获取优惠券列表
//        List<Map<String, Object>> couponList = couponMapper.getCouponListByUserId(userId);
//        // 当本次骑行订单金额大于优惠券金额
//        Date endTime = new Date();
//        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
//        Date startTime = null;
//        try {
//            startTime = format.parse((String) orderData.get("order_createtime"));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//
//        double riding_time = (endTime.getTime() - startTime.getTime()) / 1000; // 骑行时常（秒）
//        System.out.println("ridingTime: " + riding_time);
//        long riding_unit = (long) Math.ceil(riding_time / 60 / 30); // 多少个30分钟
//        System.out.println("riding_unit: " + riding_unit);
//        double riding_cost = 0.5 * riding_unit; // 骑行费用（元）
//        riding_cost = riding_cost > 2 ? 2 : riding_cost; // 最多2元
//        System.out.println("riding_cost: " + riding_cost);
//        double couponMoney = 0;
//        if (couponList.size() > 0) {
//            // 使用优惠券
//            orderParams.put("coupon_no", couponList.get(0).get("couponNo"));
//            // 更改优惠券状态
//            String couponNo = couponList.get(0).get("couponNo").toString();
//            System.out.println("优惠劵: " + couponNo + " 已使用");
//            couponMapper.updateCouponByCouponNo(couponNo);
//            // 获取优惠券金额
//            couponMoney = Double.parseDouble(couponList.get(0).get("couponMoney").toString());
//
//            if (riding_cost > couponMoney) {
//                riding_cost = riding_cost - couponMoney;
//                // 查询用户余额
//                Map<String, Object> userSearchParams = new HashMap<String, Object>();
//                userSearchParams.put("user_id", userId);
//                Map<String, Object> accountData = userMapper.getUserAccount(userSearchParams);
//                /// 扣除用户余额并更新表
//                setUserAccount(accountData, riding_cost, userId);
//            }else {
//                // 查询用户余额
//                Map<String, Object> userSearchParams = new HashMap<String, Object>();
//                userSearchParams.put("user_id", userId);
//                Map<String, Object> accountData = userMapper.getUserAccount(userSearchParams);
//
//                // 扣除用户余额并更新表
//                count+=setUserAccount(accountData, riding_cost, userId);
//            }
//        }else{
//            // 查询用户余额
//            Map<String, Object> userSearchParams = new HashMap<String, Object>();
//            userSearchParams.put("user_id", userId);
//            Map<String, Object> accountData = userMapper.getUserAccount(userSearchParams);
//
//            // 扣除用户余额并更新表
//            count+=setUserAccount(accountData, riding_cost, userId);
//        }
//
//        // 更新订单
//        orderParams.put("coupon_cost", couponMoney);//优惠卷金额
//        orderParams.put("order_endtime", endTime);
//        orderParams.put("riding_time", riding_time);
//        orderParams.put("riding_cost", riding_cost);
//        orderParams.put("riding_distance",params.get("ridingDistance"));
//        orderParams.put("carbon_saving",params.get("carbonSaving"));
//        orderParams.put("consume_kcal",params.get("consumeKcal"));
//        orderParams.put("riding_paths",params.get("ridingPaths"));
//        orderParams.put("riding_order_id", params.get("ridingOrderId"));
//        orderMapper.completeOrder(orderParams);
//        System.out.println("更新订单：" + params.get("ridingOrderId") + " 状态改为已完成");
//        if(   !params.get("ridingDistance").toString().equals("0")||
//                !params.get("carbonSaving").toString().equals("0")||
//                !params.get("consumeKcal").toString().equals("0")
//                ) {
//            count += userMapper.updateUserInfo(params);
//        }
//        return count;
        int count = 0 ;
        Map<String, Object> orderParams = new HashMap<String, Object>();
        orderParams.put("riding_paths",params.get("ridingPaths"));
        orderParams.put("riding_order_id", params.get("ridingOrderId"));
        orderParams.put("riding_distance", params.get("ridingDistance"));
        orderParams.put("carbon_saving", params.get("carbonSaving"));
        orderParams.put("consume_kcal", params.get("consumeKcal"));
        orderMapper.completeOrder(orderParams);
        if(   !params.get("ridingDistance").toString().equals("0")||
                !params.get("carbonSaving").toString().equals("0")||
                !params.get("consumeKcal").toString().equals("0")
                ) {
            count += userMapper.updateUserInfo(params);
        }
        return count;
    }
    // 扣除用户余额
    private int setUserAccount (Map<String, Object> accountData, double riding_cost, Integer userId) {
        BigDecimal account = (BigDecimal) accountData.get("user_account");
        BigDecimal result = account.subtract(new BigDecimal(riding_cost));
        Map<String, Object> userAccountParams = new HashMap<String, Object>();
        userAccountParams.put("userId", userId);
        userAccountParams.put("user_account", result.doubleValue());
        return userMapper.setUserAccount(userAccountParams);
    }
    @Override
	public Map<String,Object> getByUserTel(String userTel) {
		return userMapper.getByUserTel(userTel);
	}
}
